# -*- coding:utf8 -*-
__author__ = 'Forcs'

from django.shortcuts import render_to_response
from django.conf.urls import url
import urllib
import json

from wancake import config
from wancake.view.render import DashboardRender

from wanbatvcom.http.whttp import send_ok_response_with_data, send_error_response_with_message

def _render(view_name, request, context=None):
    session_id = request.GET["session"]
    render = DashboardRender(view_name, session_id=session_id)
    return render.render(request, context=context)

class WeixinStatisManagement(object):

    def __init__(self):
        self._region_url_map = {}

    @property
    def urls(self):
        urlpatterns = [
            url(r'^index$', self.index, name="wx_index"),
            url(r'^statis$', self.get_weixin_statis, name="wx_statis"),
        ]

        return urlpatterns

    def index(self, request):
        return _render("weixin_index.html", request)

    def get_weixin_statis(self, request):
        data_url = "http://121.201.14.248/album/get_weixin_data.jsp"
        try:
            io_stream = urllib.urlopen(data_url)
            content = io_stream.read()
            content = content.replace('\r\n', '')
            content = content.replace('\t', '')
            io_stream.close()

            return send_ok_response_with_data(data={
                "list": json.loads(content)
            })
        except Exception as e:
            return send_error_response_with_message(message=e.message)




weixin = WeixinStatisManagement()
